<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="发票抬头" prop="headerTitle">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.headerTitle"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="companyName">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.companyName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社会统一信用代码" prop="usci">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.usci"
          placeholder="请输入社会统一信用代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['biz:bizInvoiceHeader:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:bizInvoiceHeader:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:bizInvoiceHeader:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['biz:bizInvoiceHeader:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bizInvoiceHeaderList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="序号" type="index" align="left" width="60" />
      <el-table-column label="发票抬头" align="left" prop="headerTitle" min-width="250" show-overflow-tooltip/>
      <el-table-column label="抬头类型" align="left" prop="headerType" width="100">
        <template #default="scope">
          <dict-tag :options="invoice_header_type" :value="scope.row.headerType"/>
        </template>
      </el-table-column>
      <el-table-column label="发票类型" align="left" prop="invoiceType" width="150">
        <template #default="scope">
          <dict-tag :options="invoice_type" :value="scope.row.invoiceType"/>
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="left" prop="companyName" min-width="250" show-overflow-tooltip/>
      <el-table-column label="社会统一信用代码" align="left" prop="usci" min-width="250" show-overflow-tooltip/>
      <el-table-column label="开户银行" align="left" prop="depositBank" width="180" show-overflow-tooltip/>
      <el-table-column label="开户行账号" align="left" prop="cardNumber" width="180" show-overflow-tooltip/>
      <el-table-column label="注册地址" align="left" prop="registeredAddr" width="300" show-overflow-tooltip/>
      <el-table-column label="联系人" align="left" prop="contacts" width="120"/>
      <el-table-column label="联系电话" align="left" prop="phone" width="120"/>
      <el-table-column fixed="right" label="操作" align="left" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:bizInvoiceHeader:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:bizInvoiceHeader:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票抬头对话框 -->
    <el-dialog :close-on-click-modal="false" draggable :title="title" v-model="open" width="800px" append-to-body @close="cancel">
      <el-form ref="bizInvoiceHeaderRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="发票抬头" prop="headerTitle">
          <el-input maxlength="50" v-model="form.headerTitle" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="抬头类型" prop="headerType">
          <el-select v-model="form.headerType" placeholder="请选择抬头类型">
            <el-option
              v-for="dict in invoice_header_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select v-model="form.invoiceType" placeholder="请选择发票类型">
            <el-option
              v-for="dict in invoice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-input maxlength="50" v-model="form.companyName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="社会统一信用代码" prop="usci">
          <el-input maxlength="50" v-model="form.usci" placeholder="请输入社会统一信用代码" />
        </el-form-item>
        <el-form-item label="开户银行" prop="depositBank">
          <el-input maxlength="50" v-model="form.depositBank" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="开户行账号" prop="cardNumber">
          <el-input maxlength="50" v-model="form.cardNumber" placeholder="请输入开户行账号" />
        </el-form-item>
        <el-form-item label="注册地址" prop="registeredAddr">
          <el-input maxlength="50" v-model="form.registeredAddr" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input maxlength="50" v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input maxlength="50" v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BizInvoiceHeader">
import { listBizInvoiceHeader, getBizInvoiceHeader, delBizInvoiceHeader, addBizInvoiceHeader, updateBizInvoiceHeader } from "@/api/biz/bizInvoiceHeader";
import {currentDate} from '@/utils/dateutil';
import {getInvoiceDefault} from '@/api/api'

const { proxy } = getCurrentInstance();
const { invoice_header_type, invoice_type } = proxy.useDict('invoice_header_type', 'invoice_type');

const bizInvoiceHeaderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    headerTitle: null,
    companyName: null,
    usci: null,
    contacts: null,
  },
  rules: {
    headerTitle: [
      { required: true, message: "发票抬头不能为空", trigger: "submit" }
    ],
    headerType: [
      { required: true, message: "抬头类型不能为空", trigger: "submit" }
    ],
    invoiceType: [
      { required: true, message: "发票类型不能为空", trigger: "submit" }
    ],
    companyName: [
      { required: true, message: "单位名称不能为空", trigger: "submit" }
    ],
    usci: [
      { required: true, message: "社会统一信用代码不能为空", trigger: "submit" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询发票抬头列表 */
function getList() {
  loading.value = true;
  listBizInvoiceHeader(queryParams.value).then(response => {
    bizInvoiceHeaderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    updateTime: null,
    deleted: null,
    headerTitle: null,
    headerType: null,
    invoiceType: null,
    companyName: null,
    usci: null,
    depositBank: null,
    cardNumber: null,
    registeredAddr: null,
    contacts: null,
    phone: null
  };
  proxy.resetForm("bizInvoiceHeaderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加发票抬头";
  getInvoiceDefault().then(response => {
    form.value.headerType = response.data;
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBizInvoiceHeader(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改发票抬头";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bizInvoiceHeaderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBizInvoiceHeader(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBizInvoiceHeader(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除发票抬头数据项？').then(function() {
    return delBizInvoiceHeader(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('biz/bizInvoiceHeader/export', {
    ...queryParams.value
  }, `发票抬头-${currentDate()}.xlsx`)
}

getList();
</script>
